package com.lic.system.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Hewb
 * @ClassName: MetaHandler
 * @Description: TODO  提取公共属性自动注入,如新增时间、更新时间等
 * @date: 2020/6/29 14:52
 * @since JDK 1.8
 */
@Component
@Slf4j
public class MetaHandler implements MetaObjectHandler {
    @Value("${expire.time}")
    private  Integer time;
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            Date time = new Date();
            if (metaObject.hasSetter("createTime")) {
                log.debug("自动插入 createTime");
                this.setFieldValByName("createTime", time, metaObject);
            }
        } catch (Exception e) {
            log.error("自动注入失败:{}", e);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            Date time1 = new Date();
            if (metaObject.hasSetter("expireTime")) {
                log.debug("自动插入 lastTime  上次登录时间");
                Date expireTime = new Date(time1.getTime() + time* 1000);
                this.setFieldValByName("expireTime", expireTime, metaObject);
            }

        } catch (Exception e) {
            log.error("自动注入失败:{}", e);
        }
    }
}
